package myky.com.doctorapp.utils;

import android.os.AsyncTask;
import android.os.CountDownTimer;
import android.widget.Button;

/**
 * Created by 61904 on 2017/7/24.
 * 倒计时工具类
 */

public class CountDownTimerUtils extends CountDownTimer {
    private Button button;
    private static boolean isClick=true;
    private CountDownTimerUtils countDownTimerUtils;

    /**
     * @param millisInFuture    The number of millis in the future from the call
     *                          to {@link #start()} until the countdown is done and {@link #onFinish()}
     *                          is called.
     * @param countDownInterval The interval along the way to receive
     *                          {@link #onTick(long)} callbacks.
     */
    public CountDownTimerUtils(Button button, long millisInFuture, long countDownInterval) {
        super(millisInFuture, countDownInterval);
        this.button=button;
    }


    @Override
    public void onTick(long millisUntilFinished) {
        button.setClickable(false);
        button.setSelected(false);
        button.setText("重新发送("+millisUntilFinished/1000+"s)");

    }


    @Override
    public void onFinish() {
        button.setClickable(true);
        button.setSelected(true);
        button.setText("重新发送");

    }



    /**
     * 倒计时方法
     */
    public static boolean countDown() {


        if (isClick=false){
            return false;
        }
        AsyncTask<Void, Integer, Void> task = new AsyncTask<Void, Integer, Void>() {


            @Override
            protected Void doInBackground(Void... params) {
                for (int i = 3; i > 0; i--) {
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    publishProgress(i);
                }
                return null;
            }

            @Override
            protected void onProgressUpdate(Integer... values) {
                super.onProgressUpdate(values);

                if (values[0] == 1) {
                   isClick=true;
                }else {
                   isClick=false;
                }
            }

        };
        task.execute();

        return isClick;
    }



}
